package com.imti.mall.order.controller;

import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.model.oms.pojo.OmsOrder;
import com.imti.mall.model.oms.pojo.OmsOrderSetting;
import com.imti.mall.model.oms.query.OmsOrderListParam;
import com.imti.mall.model.oms.vo.OmsOrderDetail;
import com.imti.mall.order.service.DeliveryOrderService;
import com.imti.mall.order.service.OmsOrderService;
import com.imti.mall.order.service.OrderSettingService;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags="订单管理")
@RequestMapping("api/v1")
public class OrderController {
   @Resource
   private OmsOrderService omsOrderService;
    @Resource
    private DeliveryOrderService deliveryOrderService;
    @GetMapping("/orders/fetchOrderList")
    public CommonResult fetchOrderList(OmsOrder omsOrder,OmsOrderListParam omsOrderListParam){
        Map<String,Object> map=new HashMap<>();
        omsOrderListParam.setPageIndex();
        map.put("omsOrder",omsOrder);
        map.put("orderParam",omsOrderListParam);
        List<OmsOrder> list= omsOrderService.fetchOrderList(map);
        return CommonResult.success(list);
    }

    @PostMapping("/orders/closeOrder")
    public CommonResult closeOrder(Integer[] ids,String note){//批量关闭订单

        Map<String,Object> map=new HashMap<>();

        List<Integer> idList=new ArrayList<>();
        for(Integer orderId:ids){
            OmsOrderDetail omsOrder= omsOrderService.getOrderDetails(orderId);
            if(omsOrder.getStatus()>=3){
                idList.add(orderId);
            }
        }
        map.put("note",note);
        map.put("idList",idList);
        Integer flag=0;
         if(idList.size()>0){
             flag=omsOrderService.closeOrder(map);
             return CommonResult.success(flag==idList.size());
         }else{
             return CommonResult.success(false);
         }

    }
    @PostMapping("/orders/deleteOrder")
    public CommonResult deleteOrder(Integer[] ids){//批量删除订单
        List<Integer> idList=new ArrayList<>();
        for(Integer orderId:ids){
            OmsOrderDetail omsOrder= omsOrderService.getOrderDetails(orderId);
            if(omsOrder.getStatus()>=4){
                idList.add(orderId);
            }
        }
        if(idList.size()>0){
            Integer flag=omsOrderService.deleteOrder(idList);

            return CommonResult.success(flag>0);
        }else{
            return CommonResult.success(false);
        }

    }

    @PostMapping("/orders/updateDelivery")
    public CommonResult updateDelivery(@RequestBody List<OmsOrder> omsOrder){//批量发送订单
        int sum=0;

        for(OmsOrder order:omsOrder){
            OmsOrderDetail order1= omsOrderService.getOrderDetails(order.getOrderId().intValue());
            if(order1.getStatus()==1){
                sum+=deliveryOrderService.deliveryOrder(order);
            }

        }
        return CommonResult.success(sum>0);
    }

    @GetMapping("/orders/{orderId}")
    public CommonResult getOrderDetails(@PathVariable Integer orderId){
        OmsOrderDetail omsOrder= omsOrderService.getOrderDetails(orderId);
        return CommonResult.success(omsOrder);
    }

    @PostMapping("/orders/receiverInfo")
    public CommonResult updateReceiverInfo(@RequestBody OmsOrder omsOrder){//发送订单
        int sum=deliveryOrderService.updateReceiverInfo(omsOrder);

        return CommonResult.success(sum>0);
    }

    @PostMapping("/orders/updateMoney")
    public CommonResult updateMoney(@RequestBody OmsOrder omsOrder){
        int sum=deliveryOrderService.updateMoney(omsOrder);

        return CommonResult.success(sum>0);
    }


    @PostMapping("/orders/noteOrder")
    public CommonResult noteOrder(@RequestBody OmsOrder omsOrder){
        int sum=deliveryOrderService.updateOrderNote(omsOrder);

        return CommonResult.success(sum>0);
    }



}
